package com.ysq.storage.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ysq.storage.mapper.ProductCategoryMapper;
import com.ysq.storage.domain.ProductCategory;
import com.ysq.storage.service.IProductCategoryService;
import org.springframework.util.ObjectUtils;

/**
 * 商品分类Service业务层处理
 *
 * @author ysq
 * @date 2025-06-20
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param categoryId 商品分类主键
     * @return 商品分类
     */
    @Override
    public ProductCategory selectProductCategoryByCategoryId(Long categoryId) {
        return productCategoryMapper.selectProductCategoryByCategoryId(categoryId);
    }

    /**
     * 查询商品分类列表
     *
     * @param productCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        return productCategoryMapper.selectProductCategoryList(productCategory);
    }
    /**
     * 查询商品分类列表
     *
     * @param productCategory 商品分类
     * @return 商品分类树
     */
    @Override
    public List<ProductCategory> selectProductCategoryTree(ProductCategory productCategory) {
        List<ProductCategory> productCategories = productCategoryMapper.selectProductCategoryList(productCategory);
        Map<Long, ProductCategory> nodeMap = new HashMap<>();
        List<ProductCategory> roots = new ArrayList<>();

        // 第一次遍历：建立ID映射
        for (ProductCategory node : productCategories) {
            nodeMap.put(node.getCategoryId(), node);
        }

        // 第二次遍历：构建父子关系
        for (ProductCategory node : productCategories) {
            if (node.getParentId() == null|| node.getParentId() == 0) {
                roots.add(node);
            } else {
                ProductCategory parent = nodeMap.get(node.getParentId());
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }

        return roots;
    }

    /**
     * 新增商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public boolean insertProductCategory(ProductCategory productCategory) {
        productCategory.setCategoryLevel(0);
        Long parentId = productCategory.getParentId();
        if (parentId == null || parentId <= 0) {
            ProductCategory parent = productCategoryMapper.selectById(parentId);
            if (!ObjectUtils.isEmpty( parent)){
                productCategory.setCategoryLevel(parent.getCategoryLevel() + 1);
            }

        }
        return save(productCategory);
    }

    /**
     * 修改商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateProductCategory(ProductCategory productCategory) {
        return productCategoryMapper.updateProductCategory(productCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param categoryIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByCategoryIds(Long[] categoryIds) {
        return productCategoryMapper.deleteProductCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除商品分类信息
     *
     * @param categoryId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByCategoryId(Long categoryId) {
        return productCategoryMapper.deleteProductCategoryByCategoryId(categoryId);
    }

}
